<?php

namespace app\data\controller\shop;

use app\data\service\GoodsService;
use think\admin\Controller;
use think\admin\extend\DataExtend;

/**
 * 产品分类管理
 * Class Cate
 * @package app\data\controller\shop
 */
class Cate extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'ShopGoodsCate';

    /**
     * 最大分类等级
     * @var integer
     */
    protected $cateLevel;

    /**
     * 控制器初始化
     */
    protected function initialize()
    {

    }

    /**
     * 产品分类管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = "产品分类管理";
        $query = $this->_query($this->table)->like('name')->dateBetween('create_at');
        $query->equal('status')->order('sort desc,id desc')->page(false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        $companies = GoodsService::instance()->getCompanyData();

        foreach ($data as &$vo) {
            foreach ($companies as $company) if ($company['id'] == $vo['company_id']) $vo['companyinfo'] = $company;
        }
    }

    /**
     * 添加产品分类
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑产品分类
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            // 其他表单数据
            $this->companies = GoodsService::instance()->getCompanyData();

        } elseif ($this->request->isPost()) {

            if (empty($data['name'])) $this->error('产品名称不能为空！');
            if (empty($data['company_id'])) $this->error('公司不能为空');

        }
    }

    /**
     * 修改产品分类状态
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $this->_save($this->table, $this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除产品分类
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $id = input('id');
        $goodsCount = $this->app->db->name('ShopGoods')->where('cate_id',$id)->count();
        if($goodsCount){
            $this->error('此分类下有产品，无法删除');
        }

        $this->_delete($this->table);
    }
}